﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ShkSimulation.carsharing;
using ShkSimulation.core.template.model;
using ShkSimulation.desktop.util;

namespace ShkSimulation.desktop.component.parallel {

	public partial class SingleConfigForm : Form {

		public SingleConfig SingleConfig { get; set; } = new SingleConfig();

		public SingleConfigForm(InputConfig config = null) {
			InitializeComponent();
			this.TopMost = true;
			if (config != null) {
				SingleConfig.StationFile = config.GetResourceValue(CarsharingConfig.StationResourceKey);
				SingleConfig.RoadFile = config.GetResourceValue(CarsharingConfig.RoadResourceKey);
				SingleConfig.DemandFile = config.GetResourceValue(CarsharingConfig.DemandResourceKey);
				SingleConfig.Key = config.GetConfigValue<string>(ParallelConfigPanel.CONFIG_UNIQUE_KEY);

				StationTextBox.Text = SingleConfig.StationFile;
				RoadTextBox.Text = SingleConfig.RoadFile;
				DemandTextBox.Text = SingleConfig.DemandFile;
				UniqueKeyTextBox.Text = SingleConfig.Key;
			}
			if (!string.IsNullOrEmpty(StationTextBox.Text)) {
				SingleConfig.StationFile = StationTextBox.Text;
			}			
			if (!string.IsNullOrEmpty(RoadTextBox.Text)) {
				SingleConfig.RoadFile = RoadTextBox.Text;
			}			
			if (!string.IsNullOrEmpty(DemandTextBox.Text)) {
				SingleConfig.DemandFile = DemandTextBox.Text;
			}
			if (!string.IsNullOrEmpty(UniqueKeyTextBox.Text)) {
				SingleConfig.Key = UniqueKeyTextBox.Text;
			}
		}

		private void CertainButton_Click(object sender, EventArgs e) {
			if (!FileValid(SingleConfig)) {
				Console.WriteLine(@"Nonono {0} {1} {2}\n", SingleConfig.StationFile, SingleConfig.RoadFile, SingleConfig.DemandFile);
				this.DialogResult = DialogResult.Cancel;
				SingleConfig = null;
				return;
			}
			SingleConfig.Key = UniqueKeyTextBox.Text;
			this.DialogResult = DialogResult.Yes;
			this.Close();
		}
		
		private bool FileValid(SingleConfig singleConfig) {
			return File.Exists(singleConfig.StationFile) && File.Exists(singleConfig.RoadFile) &&
			       File.Exists(singleConfig.DemandFile);
		}

		private void CancelButton_Click(object sender, EventArgs e) {
			this.DialogResult = DialogResult.Cancel;
			SingleConfig = null;
		}

		private void StationTextBox_MouseDoubleClick(object sender, MouseEventArgs e) {
			string filePath = FileUtil.OpenFile(OpenDialog, "选择站台文件", "输入数据|*.xlsx|所有文件|*.*");
			if (!string.IsNullOrEmpty(filePath)) {
				StationTextBox.Text = filePath;
				SingleConfig.StationFile = filePath;
			}
		}

		private void RoadTextBox_MouseDoubleClick(object sender, MouseEventArgs e) {
			string filePath = FileUtil.OpenFile(OpenDialog, "选择道路文件", "输入数据|*.xlsx|所有文件|*.*");
			if (!string.IsNullOrEmpty(filePath)) {
				RoadTextBox.Text = filePath;
				SingleConfig.RoadFile = filePath;
			}
		}

		private void DemandTextBox_MouseDoubleClick(object sender, MouseEventArgs e) {
			string filePath = FileUtil.OpenFile(OpenDialog, "选择需求文件", "输入数据|*.xlsx|所有文件|*.*");
			if (!string.IsNullOrEmpty(filePath)) {
				DemandTextBox.Text = filePath;
				SingleConfig.DemandFile = filePath;
			}
		}

        private void SelectFolderButton_Click(object sender, EventArgs e){
	        string folderPath = FileUtil.OpenFolder(FolderDialog, @"选择数据文件夹，文件必须是station.xlsx,road.xlsx,demand.xlsx");
	        if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath)) {
		        Console.WriteLine(@"取消或没有文件夹");
		        return;
	        }
	        DirectoryInfo info = new DirectoryInfo(folderPath);
	        string key = info.Name;
	        Console.WriteLine(key);
	        FileInfo[] files = info.GetFiles();
	        if (files.Length < 3) {
		        Console.WriteLine(@"文件信息不够");
		        return;
	        }
	        string station = @"station.xlsx";
	        string road = @"road.xlsx";
	        string demand = @"demand.xlsx";
	        Dictionary<string, FileInfo> fileExists = files.ToDictionary(each => each.Name, each => each);
	        if (!fileExists.ContainsKey(station) || !fileExists.ContainsKey(road) || !fileExists.ContainsKey(demand)) {
		        Console.WriteLine(@"文件信息不够");
		        return;
	        }
	        this.SingleConfig.Key = key;
	        this.SingleConfig.StationFile = fileExists[station].FullName;
	        this.SingleConfig.RoadFile = fileExists[road].FullName;
	        this.SingleConfig.DemandFile = fileExists[demand].FullName;
	        this.StationTextBox.Text = SingleConfig.StationFile;
	        this.RoadTextBox.Text = SingleConfig.RoadFile;
	        this.DemandTextBox.Text = SingleConfig.DemandFile;
	        this.UniqueKeyTextBox.Text = key;
        }
    }

}